package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogHrefConfig;
import com.ruoyi.blog.service.IBlogHrefConfigService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 第三方链接配置Controller
 *
 * @author 刘哲贤
 * @date 2022-08-30
 */
@RestController
@RequestMapping("/admin/config")
public class BlogHrefConfigController extends BaseController {
    @Resource
    private IBlogHrefConfigService blogHrefConfigService;

    /**
     * 查询第三方链接配置列表
     */
    @PreAuthorize("@ss.hasPermi('admin:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogHrefConfig blogHrefConfig) {
        startPage();
        List<BlogHrefConfig> list = blogHrefConfigService.selectBlogHrefConfigList(blogHrefConfig);
        return getDataTable(list);
    }

    /**
     * 导出第三方链接配置列表
     */
    @PreAuthorize("@ss.hasPermi('admin:config:export')")
    @Log(title = "第三方链接配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogHrefConfig blogHrefConfig) {
        List<BlogHrefConfig> list = blogHrefConfigService.selectBlogHrefConfigList(blogHrefConfig);
        ExcelUtils<BlogHrefConfig> util = new ExcelUtils<BlogHrefConfig>(BlogHrefConfig.class);
        util.exportData(response, list, "第三方链接配置数据");
    }

    /**
     * 获取第三方链接配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(blogHrefConfigService.selectBlogHrefConfigById(id));
    }

    /**
     * 新增第三方链接配置
     */
    @PreAuthorize("@ss.hasPermi('admin:config:add')")
    @Log(title = "第三方链接配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogHrefConfig blogHrefConfig) {
        blogHrefConfig.setCreateTime(new Date());
        return toAjax(blogHrefConfigService.insertBlogHrefConfig(blogHrefConfig));
    }

    /**
     * 修改第三方链接配置
     */
    @PreAuthorize("@ss.hasPermi('admin:config:edit')")
    @Log(title = "第三方链接配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogHrefConfig blogHrefConfig) {
        return toAjax(blogHrefConfigService.updateBlogHrefConfig(blogHrefConfig));
    }

    /**
     * 删除第三方链接配置
     */
    @PreAuthorize("@ss.hasPermi('admin:config:remove')")
    @Log(title = "第三方链接配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blogHrefConfigService.deleteBlogHrefConfigByIds(ids));
    }
}
